package com.bw.controller;

import com.bw.domain.Role;
import com.bw.domain.User;
import com.bw.service.UserService;
import com.bw.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: TaoShiHao
 * @Date: 2025/3/12 19:39
 * @Description:
 **/
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("tolist")
    public String tolist() {
        return "user/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public R list(User user, Integer page, Integer limit, HttpSession session) {
        User sessionUser = (User) session.getAttribute("user");

        List<Role> roleList = (List<Role>) redisTemplate.opsForValue().get("role:" + sessionUser.getId());
        String roleName = "";
        String authId = "";
        for (Role role : roleList) {
            roleName += role.getRoleName();
            authId += "," + role.getId();
        }
        authId = authId.substring(1);
        if (!roleName.contains("超级管理员")) {
            user.setAuthId(authId);
        }
        PageHelper.startPage(page, limit);
        List<User> userList = userService.list(user);
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return R.OK(userPageInfo);
    }

    @RequestMapping("toadd")
    public String toadd() {
        return "/user/add";
    }

    @ResponseBody
    @RequestMapping("listRole")
    public R listRole() {
        List<Role> roleList = userService.listRole();
        return R.OK(roleList);
    }

    @ResponseBody
    @RequestMapping("checkName")
    public R checkName(String username) {
        int i = userService.checkName(username);
        return i > 0 ? R.ERROR(500, "人员姓名已存在") : R.OK();
    }

    @ResponseBody
    @RequestMapping("add")
    public R add(User user) {
        if (StringUtils.isEmpty(user.getUsername())) {
            return R.ERROR(500, "用户名不能为空");
        }
        if (StringUtils.isEmpty(user.getLoginName())) {
            return R.ERROR(500, "登陆账户不能为空");
        }
        if (StringUtils.isEmpty(user.getPhone())) {
            return R.ERROR(500, "手机号不能为空");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return R.ERROR(500, "密码不能为空");
        }
        if (StringUtils.isEmpty(user.getEmail())) {
            return R.ERROR(500, "邮箱不能为空");
        }
        if (StringUtils.isEmpty(user.getAuthId())) {
            return R.ERROR(500, "角色不能为空");
        }
        int i = userService.checkName(user.getUsername());
        if (i > 0) {
            return R.ERROR(500, "人员姓名已存在");
        }

        boolean b = userService.add(user);

        return b ? R.OK() : R.ERROR(500, "添加失败");
    }
}
